import { Component,Input } from '@angular/core';
import { IonicPage, NavController, NavParams,ModalController,LoadingController } from 'ionic-angular';
import { BaseUI } from '../../commom/baseui';
import {RestProvider} from '../../providers/rest/rest'
import {DetailsPage} from '../details/details'
import {ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';
import {Storage} from '@ionic/storage'

/**
 * Generated class for the DiscoveryPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()

@Component({
  selector: 'page-discovery',
  templateUrl: 'discovery.html',
})
export class DiscoveryPage extends BaseUI{

  @Input() inputSrc: string;
  //public placeholderActive: boolean = true;
  questions: string[];
  errorMessage: any;
  imgSrc : string [] = ["https://img.mukewang.com/5bfdfc160001e42f18720632.jpg","https://img.mukewang.com/5bfd65a9000124de18720632.jpg","https://img.mukewang.com/5bfd65f10001fd4018720632.jpg"];
  constructor(public navCtrl: NavController,
              public modalCtrl:ModalController,
              public loadingCtrl:LoadingController,
              public rest:RestProvider,
              public storage:Storage
              ) {
              super()

}
@ViewChild(Slides) slides: Slides;
  ionViewDidLoad() {
    this.loadingQusetions();
  }

  ionViewWillEnter(){
    this.slides.startAutoplay();
  }
  ionViewWillLeave(){
    this.slides.stopAutoplay();
  }
  loadingQusetions(){
    this.storage.get('questions').then((val)=>{
      if (val !=null){
        this.questions = val
      }else{
        this.getQuestions
      }
    })
  }
  getQuestions() {
    var loading = super.showLoading(this.loadingCtrl,"加载中...");
    this.rest.getQuestions()
    .subscribe(
      q=>{
        this.storage.remove('questions')
        this.questions = q;
        this.storage.set('questions', q);
        loading.dismiss();
      },
      error =>this.errorMessage = <any>error)

  }

  doRefresh(refresher){
    this.getQuestions();
    refresher.complete();
  }

  gotoDetails(questionId){
    this.navCtrl.push(DetailsPage,{id:questionId})

  }

}
